package org.firebug.common.stock.service;

import com.alibaba.fastjson.JSON;
import org.firebug.common.stock.bean.SohuResponse;
import org.firebug.common.stock.entity.StockEntity;
import org.firebug.common.stock.entity.TradeEntity;
import org.firebug.common.stock.mapper.StockMapper;
import org.firebug.common.stock.mapper.TradeMapper;
import org.firebug.common.tools.net.HttpTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TradeService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private TradeMapper tradeMapper;
    @Resource
    private StockMapper stockMapper;

    private List<TradeEntity> curlFromSohu(List<String> codes, String start, String end) throws IOException {
        String cn_code = StringUtils.collectionToDelimitedString(codes, ",cn_", "cn_", "");
        String url = "https://q.stock.sohu.com/hisHq?code=" + cn_code + "&start=" + start + "&end=" + end + "&order=A";
        logger.info(url);
        List<TradeEntity> entities = new LinkedList<>();
        String responseJson = HttpTools.get(url);
        List<SohuResponse> responses = JSON.parseArray(responseJson, SohuResponse.class);
        for(SohuResponse response : responses) {
            if (response.success()) {
                String code = response.getCode().replaceAll("cn_","");
                String[][] records = response.getHq();
                for (String[] record : records) {
                    TradeEntity entity = new TradeEntity();
                    entity.setCode(code);
                    entity.setDate(Integer.parseInt(record[0].replace("-", "")));
                    entity.setOpen(new BigDecimal(record[1]));
                    entity.setClose(new BigDecimal(record[2]));
                    entity.setPriceChange(new BigDecimal(record[3]));
                    entity.setQuoteChange(new BigDecimal(record[4].replace("%", "")));
                    entity.setLower(new BigDecimal(record[5]));
                    entity.setUpper(new BigDecimal(record[6]));
                    entity.setVolume(Long.parseLong(record[7]));
                    entity.setAmount(new BigDecimal(record[8]));
                    entity.setTurnover(new BigDecimal(record[9]));
                    entities.add(entity);
                }
            }
        }
        return entities;
    }

    public void crawl(List<String> codes, String start, String end) throws IOException {
        List<TradeEntity> entities = curlFromSohu(codes, start, end);
        tradeMapper.saveAll(entities);
    }

    public void crawl(String date) throws IOException {
        List<String> codes = this.findAllCodes();
        List<TradeEntity> entities = curlFromSohu(codes, date, date);
        tradeMapper.saveAll(entities);
    }

    public List<String> findAllCodes() {
        List<StockEntity> stocks = stockMapper.findAll();
        return stocks.stream().map(t -> t.getCode()).collect(Collectors.toList());
    }
}
